package com.gusto.utils.cli;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;

import com.gusto.engine.repository.exception.RepositoryException;
import com.gusto.engine.repository.service.RepositoryService;
import com.gusto.engine.semsim.exceptions.ConfigException;
import com.gusto.engine.beans.exceptions.ResCreationException;
import com.gusto.engine.semsim.exceptions.SimilarityException;
import com.gusto.engine.semsim.service.SimilarityService;
import com.gusto.utils.semantic.SemanticService;

public abstract class SemSimCLI {
	
	private SimilarityService semanticSimilarity;
	private SemanticService semanticService;
	
	public SemSimCLI(SimilarityService semanticSimilarity, RepositoryService modelService, SemanticService semanticService) {
		this.semanticSimilarity = semanticSimilarity;
		this.semanticService = semanticService;
	}
	
	public abstract void load(String file);
	
	public abstract void resource(String resource);
	
	public void query(String file) throws SimilarityException {
		System.out.println(semanticService.sparqlQuery(file));
	}
	
	public abstract void similarity(String res1, String res2) throws SimilarityException, ResCreationException, RepositoryException;
	
	public abstract void neighborhood(String resource) throws SimilarityException, ResCreationException, RepositoryException;
	
	public abstract void help();
	
	public void execute(String[] args) throws SimilarityException, ResCreationException, RepositoryException, ConfigException, SQLException, IOException {
		System.out.println("check...");
		try {
			semanticSimilarity.checkDefinitionChanged();
		}
		catch (Exception ex) {
			ex.printStackTrace();
		}
		
		if (false) {
		//if (args.length > 0) {
			System.out.println("Unique command ...");
			System.out.print("command...");
			processCommand(args);
		}
		else {
			System.out.println("Application ...");
			while (true) {
				System.out.print("command...");
				BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
				String string = br.readLine();
				String[] completeCommand = string.split(" ");
				processCommand(completeCommand);
			}
		}
	}
	
	public void processCommand(String[] completeCommand) throws SimilarityException, ResCreationException, RepositoryException {
		if (completeCommand.length > 0) {
			String cmd = completeCommand[0];
			if (cmd.equals("exit") && (completeCommand.length == 1)) {
				System.exit(0);
			}
			else if (cmd.equals("load") && (completeCommand.length == 2)) {
				load(completeCommand[1]);
			}
			else if(cmd.equals("resource") && (completeCommand.length == 2)) {
				resource(completeCommand[1]);
			}
			else if(cmd.equals("query") && (completeCommand.length == 2)) {
				query(completeCommand[1]);
			}
			else if(cmd.equals("similarity") && (completeCommand.length == 3)) {
				similarity(completeCommand[1], completeCommand[2]);
			}
			else if(cmd.equals("neighborhood") && (completeCommand.length == 2)) {
				neighborhood(completeCommand[1]);
			}
			else {
				help();
			}
		}
		else {
			help();
		}
	}
	
}
